package com.ruoyi.project.system.memberbusiness.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.companyindustry.domain.CompanyIndustry;
import com.ruoyi.project.jiaqin.companyindustry.service.ICompanyIndustryService;
import com.ruoyi.project.system.memberbusiness.domain.MemberBusiness;
import com.ruoyi.project.system.memberbusiness.service.IMemberBusinessService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户主营业务Controller
 *
 * @author LCL
 * @date 2021-11-04
 */
@Controller
@RequestMapping("/system/memberbusiness")
public class MemberBusinessController extends BaseController
{
    private String prefix = "system/memberbusiness";

    @Autowired
    private IMemberBusinessService memberBusinessService;

    @RequiresPermissions("system:memberbusiness:view")
    @GetMapping()
    public String memberbusiness()
    {
        return prefix + "/memberbusiness";
    }

    /**
     * 查询用户主营业务列表
     */
    @RequiresPermissions("system:memberbusiness:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MemberBusiness memberBusiness)
    {
        startPage();
        List<MemberBusiness> list = memberBusinessService.selectMemberBusinessList(memberBusiness);
        return getDataTable(list);
    }

    /**
     * 导出用户主营业务列表
     */
    @RequiresPermissions("system:memberbusiness:export")
    @Log(title = "用户主营业务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MemberBusiness memberBusiness)
    {
        List<MemberBusiness> list = memberBusinessService.selectMemberBusinessList(memberBusiness);
        ExcelUtil<MemberBusiness> util = new ExcelUtil<MemberBusiness>(MemberBusiness.class);
        return util.exportExcel(list, "memberbusiness");
    }

    @Autowired
    private ICompanyIndustryService companyIndustryService;
    /**
     * 新增用户主营业务
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        map.put("industryList",companyIndustryService.queryAll());
        return prefix + "/add";
    }

    /**
     * 新增保存用户主营业务
     */
    @RequiresPermissions("system:memberbusiness:add")
    @Log(title = "用户主营业务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MemberBusiness memberBusiness)
    {
        CompanyIndustry companyIndustry = companyIndustryService.selectCompanyIndustryById(memberBusiness.getIndustryId());
        if (companyIndustry != null) {
            memberBusiness.setIndustryName(companyIndustry.getName());
        }
        return toAjax(memberBusinessService.insertMemberBusiness(memberBusiness));
    }

    /**
     * 修改用户主营业务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        MemberBusiness memberBusiness = memberBusinessService.selectMemberBusinessById(id);
        mmap.put("memberBusiness", memberBusiness);
        mmap.put("industryList",companyIndustryService.queryAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存用户主营业务
     */
    @RequiresPermissions("system:memberbusiness:edit")
    @Log(title = "用户主营业务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MemberBusiness memberBusiness)
    {
        CompanyIndustry companyIndustry = companyIndustryService.selectCompanyIndustryById(memberBusiness.getIndustryId());
        if (companyIndustry != null) {
            memberBusiness.setIndustryName(companyIndustry.getName());
        }
        return toAjax(memberBusinessService.updateMemberBusiness(memberBusiness));
    }

    /**
     * 删除用户主营业务
     */
    @RequiresPermissions("system:memberbusiness:remove")
    @Log(title = "用户主营业务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(memberBusinessService.deleteMemberBusinessByIds(ids));
    }
}
